#include <vector>
#include <algorithm>
#include <iostream>

using namespace std;

vector<int> moveElementToEnd(vector<int> arr, int k) {
  int i = 0;
  int j = arr.size() - 1;

  while (i < j) {
    while (i < j && arr[j] == k) {
      --j;
    }

    if (arr[i] == k) {
      swap(arr[i], arr[j]);
    }

    ++i;
  }

  return arr;
}

int main(int argc, char const *argv[])
{
  /* code */
  vector<int> arr {2, 1, 2, 2, 2, 3, 4, 2};
  auto result = moveElementToEnd(arr, 2);

  for (int n : result) {
    cout << n << " ";
  }

  cout << endl;
  return 0;
}

